package org.nanyuan.forum.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.nanyuan.forum.common.AppResult;
import org.nanyuan.forum.common.ResultCode;
import org.nanyuan.forum.dao.ArticleReplyMapper;
import org.nanyuan.forum.exception.ApplicationException;
import org.nanyuan.forum.model.ArticleReply;
import org.nanyuan.forum.service.IArticleReplyService;
import org.nanyuan.forum.service.IArticleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private IArticleService articleService;

    @Override
    @Transactional
    public void create(ArticleReply articleReply) {
        //非空校验
        if (articleReply == null || articleReply.getArticleId() == null
        || articleReply.getPostUserId() == null || !StringUtils.hasLength(articleReply.getContent())) {

            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteFlag((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        //写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);
        if (row != 1) {
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

        //更新帖子表中的回复数
        articleService.addOneReplyCountById(articleReply.getArticleId());

        //打印日志
        log.info("userId:{}，帖子回复成功,articleId:{}，",articleReply.getPostUserId(),articleReply.getArticleId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //非空校验
        if (articleId == null || articleId <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        //返回结果
        return result;
    }
}
